﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRoom.NumberRoom;
using YJKRoom.NumberRoomControler;
using YJKView;

namespace YJKRoom.VolumeNumberRoom
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdVolumeNumberRoom : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			CmdViewType types = (CmdViewType)5;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			VolumeNumberForm volumeNumberForm = new VolumeNumberForm();
			string text = null;
			VolumeNumberControler volumeNumberControler = new VolumeNumberControler(cmdData);
			string roomTagFirstName = volumeNumberForm.RoomTagFirstName;
			string roomTagSecondName = volumeNumberForm.RoomTagSecondName;
			int roomTagNumber = volumeNumberForm.RoomTagNumber;
			int angle = volumeNumberForm.Angle;
			int sortWay = volumeNumberForm.SortWay;
			for (;;)
			{
				volumeNumberForm.RoomTagFirstName = roomTagFirstName;
				volumeNumberForm.RoomTagSecondName = roomTagSecondName;
				volumeNumberForm.RoomTagNumber = roomTagNumber;
				volumeNumberForm.Angle = angle;
				volumeNumberForm.SortWay = sortWay;
				if (volumeNumberForm.ShowDialog() != DialogResult.OK)
				{
					break;
				}
				roomTagFirstName = volumeNumberForm.RoomTagFirstName;
				roomTagSecondName = volumeNumberForm.RoomTagSecondName;
				roomTagNumber = volumeNumberForm.RoomTagNumber;
				angle = volumeNumberForm.Angle;
				sortWay = volumeNumberForm.SortWay;
				VolumeNumberControler.ResultType resultType = volumeNumberControler.CreateLabels(roomTagFirstName, roomTagNumber, roomTagSecondName, angle, sortWay, ref text);
				if (resultType == VolumeNumberControler.ResultType.RT_Cancel)
				{
					break;
				}
				if (resultType == VolumeNumberControler.ResultType.RT_Exception)
				{
					MessageBox.Show(text, "盈建科提示");
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
